﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Oliving.FormCommon.Oliving.PC
{
    //class RegistryHelper
    //{
    //}


    /// <summary>
    /// 注册表操作类
    /// </summary>
    public class RegistryHelper
    {
        /// <summary>
        /// 读取指定名称的注册表的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetRegistryData(RegistryKey root, string subkey, string name)
        {
            string registData = "";
            RegistryKey myKey = root.OpenSubKey(subkey, true);
            if (myKey != null)
            {
                registData = myKey.GetValue(name).ToString();
            }

            return registData;
        }

        /// <summary>
        /// 向注册表中写数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="tovalue"></param> 
        public void SetRegistryData(RegistryKey root, string subkey, string name, string value, RegistryValueKind type)
        {
            RegistryKey aimdir = root.CreateSubKey(subkey);
            aimdir.SetValue(name, value, type);
        }

        /// <summary>
        /// 删除注册表中指定的注册表项
        /// </summary>
        /// <param name="name"></param>
        public void DeleteRegist(RegistryKey root, string subkey, string name)
        {
            string[] subkeyNames;
            RegistryKey myKey = root.OpenSubKey(subkey, true);
            subkeyNames = myKey.GetSubKeyNames();
            foreach (string aimKey in subkeyNames)
            {
                if (aimKey == name)
                    myKey.DeleteSubKeyTree(name);
            }
        }

        /// <summary>
        /// 判断指定注册表项是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsRegistryExist(RegistryKey root, string subkey, string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey myKey = root.OpenSubKey(subkey, true);
            subkeyNames = myKey.GetSubKeyNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == name)
                {
                    _exit = true;
                    return _exit;
                }
            }

            return _exit;
        }
    }

    /// <summary>
    /// 电脑注册表操作
    /// </summary>
    public static class RegistryPCHelper
    {

        #region 远程桌面功能是

        /// <summary>
        /// 远程桌面功能是 启用状态
        /// </summary>
        /// <returns></returns>
        public static bool GetMstscState()
        {
            RegistryHelper rh = new RegistryHelper();

            string MstscValue = rh.GetRegistryData(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Terminal Server", "fDenyTSConnections");

            return MstscValue == "0";

        }

        /// <summary>
        /// 远程桌面 设置 是否启用
        /// </summary>
        /// <param name="state"></param>
        public static void SetMstscState(bool state)
        {
            RegistryHelper rh = new RegistryHelper();

            rh.SetRegistryData(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Terminal Server", "fDenyTSConnections", state ? "0" : "1", RegistryValueKind.DWord);


        }


        /// <summary>
        /// 远程桌面功能是 启用状态
        /// </summary>
        /// <returns></returns>
        //public static void SetStartPCSoft(string soft = "")
        //{
        //    RegistryHelper rh = new RegistryHelper(); 
        //    if (string.IsNullOrEmpty(soft))
        //    { soft = "explorer.exe"; } 
        //    rh.SetRegistryData(
        //        Registry.LocalMachine,
        //        @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",
        //        "shell", soft,
        //        RegistryValueKind.String); 
        //}

        #endregion

        #region 计算机启动项


        /// <summary>
        /// 计算机启动项
        ///     string startName = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationManager.AppSettings["softName"] + ".exe";
        ///     Oliving.CommonPC.Regedit.RegistryPCHelper.SetStartPCSoft(startName);
        /// </summary>
        /// <returns></returns>
        public static void SetStartPCSoft(string soft = "")
        {
            RegistryHelper rh = new RegistryHelper();

            if (string.IsNullOrEmpty(soft))
            { soft = "cmd" +
                    "" +
                    ""; }

            //rh.SetRegistryData(
            //    Registry.LocalMachine,
            //    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",
            //    "shell", soft,
            //    RegistryValueKind.ExpandString);

            try
            {
                string registData1;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software2 = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
                registData1 = software2.GetValue("Shell").ToString();
                software2.SetValue("Shell", soft);
                registData1 = software2.GetValue("Shell").ToString();
            }
            catch (Exception excp)
            {
                Debug.Write("SetStartPCSoft =>" + excp.ToString());
            }
            return;



        }



        /// <summary> 
        /// 获取计算机启动的的软件
        /// </summary>
        /// <returns></returns>
        public static string GetStartPCSoft()
        {
            RegistryHelper rh = new RegistryHelper();
            string MstscValue = rh.GetRegistryData(Registry.LocalMachine, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "shell");

            return MstscValue;
        }

        /// <summary>
        /// 计算机启动的软件项
        /// </summary>
        /// <returns></returns>
        public static bool GetStartPCAuto()
        {
            RegistryHelper rh = new RegistryHelper();
            string MstscValue = rh.GetRegistryData(Registry.LocalMachine, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "shell");

            return MstscValue.ToLower() == "explorer.exe".ToLower();
        }




        #endregion

        #region 键盘

        /// <summary>
        /// 键盘启用（重新启动计算后生效）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void KeyBoardEnable()
        {
            RegistryHelper regHelper = new RegistryHelper();

            ///\SYSTEM\CurrentControlSet\Control\Class\{4D36E96B-E325-11CE-BFC1-08002BE10318}
            ///UpperFilters
            ///SynTP
            ///kbdclass
            regHelper.SetRegistryData(
                Registry.LocalMachine,
                @"SYSTEM\CurrentControlSet\Control\Class\{4D36E96B-E325-11CE-BFC1-08002BE10318}",
                "UpperFilters",
                "kbdclass",
                RegistryValueKind.String);
        }


        /// <summary>
        ///  键盘禁用（重新启动计算后生效）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void KeyBoardDisable()
        {
            RegistryHelper regHelper = new RegistryHelper();
            regHelper.SetRegistryData(Registry.LocalMachine,
                @"SYSTEM\CurrentControlSet\Control\Class\{4D36E96B-E325-11CE-BFC1-08002BE10318}",
                "UpperFilters",
                "k.b.d.c.l.a.s.s",
                RegistryValueKind.String);
        }


        public static bool GetKeyBoardState()
        {
            RegistryHelper rh = new RegistryHelper();
            string MstscValue = rh.GetRegistryData(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Class\{4D36E96B-E325-11CE-BFC1-08002BE10318}", "UpperFilters");

            return MstscValue.ToLower().Contains("kbdclass");
        }




        #endregion


        #region 注册表-任务管理器
         

        public static void SetTaskManager(bool isEnable)
        {
            RegistryHelper regHelper = new RegistryHelper();

            regHelper.SetRegistryData(
                Registry.CurrentUser,
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System",
                "DisableTaskmgr",
                isEnable ? "0" : "1",
                RegistryValueKind.DWord);

            //RegistryKey reg = Registry.CurrentUser.CreateSubKey(@"SoftWare\Microsoft\Internet Explorer\Main", RegistryKeyPermissionCheck.ReadWriteSubTree);
            //reg.SetValue("Start Page", "http://www.baidu.com", RegistryValueKind.String);



        }

        /// <summary>
        ///  任务管理器是否启用状态
        /// </summary>
        /// <returns></returns>
        public static bool GetTaskEnable()
        {
            RegistryHelper rh = new RegistryHelper();
            string MstscValue = rh.GetRegistryData(Registry.CurrentUser, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskmgr");
            return MstscValue.ToLower() == "1".ToLower();
        }


        #endregion





    }



}
